package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class RentCollection here.
 * 
 * @author (Kasper Pedersen) 
 * @version (-2012)
 */
public class RentCollection
{
    private ArrayList<Rent> rentCollection;
    private static RentCollection instance;
    
    /**
     * Constructor for objects of class RentCollection
     */
    public RentCollection()
    {
        rentCollection = new ArrayList<Rent>();
    }
    
    public static RentCollection getInstance()
    {
        if (instance == null) {
            instance = new RentCollection();
        }
        return instance;
    }
    //Methods
    /**
     * Returns ArrayList rentCollection
     * aka getRentCollection
     */
    public ArrayList<Rent> listAllRents()
    {
        return rentCollection;
    }
    
    /**
     * Adds a Rental to the ArrayList
     * @param Rent the rental to be added
     */
    public void addRent(Rent r)
    {
        rentCollection.add(r);
    }
    
    /**
     * Finds a Rental in the ArrayList based on the Rent id
     * @param long The id of the wanted Rent
     * @return Rent the Rent found in the ArrayList
     */
    public Rent getRent(long id)
    {
        int index = 0;
        
        while (index < rentCollection.size()) {
            Rent r = rentCollection.get(index);
            if (r.getId() == id) {
                return r;
            }
            index++;
        }
        return null;
    }
    
    /**
     * Updates the rental's information. If a field is left empty, this field will not be updated
     * @param int The Due Date of which the Rental is to be delivered back
     */
    public void updateRent(long id, int dueDate)
    {
        Rent r = getRent(id);
        
        if(dueDate != 0) {r.setDueDate(dueDate);}
    }
    
     /**
     * Updates the rental's information. If a field is left empty, this field will not be updated
     * @param boolean The status of the Rental
     */
    public void updateRent(long id, boolean status, int returnDate)
    {
        Rent r = getRent(id);
        
        r.setStatus(status);
        r.setReturnDate(returnDate);
    }
    /**
     * Deletes a Rent from the ArrayList based on the id
     * @param long The id of the the Rent wanted to be deleted
     */
    public void deleteRent(long id)
    {
        int index = 0;
        boolean found = false;
        
        while (index < rentCollection.size() && !found) {
            Rent r = rentCollection.get(index);
            
            if (r.getId() == id) {
                found = true;
                rentCollection.remove(index);
            }
            index++;
        }
    }
    
    /**
     * Returns a specific rent-object, based on the person id associated with said rent-object
     * @param long This is the id of the person associated with the object
     * @return Rent This is the object which was associated with the person id.
     */
    public ArrayList<Rent> getPersonalRent(long id)
    {
        ArrayList<Rent> allRents = new ArrayList<Rent>();
        for (Rent r : rentCollection) {
            Person p = r.getPerson();
                if (p.getId() == id){
                allRents.add(r);
            }
        }
        return allRents;
    }
}
